package com.example.ordermealservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.commonutils.handler.NoqueueException;
import com.example.ordermealservice.entity.OrderClassification;
import com.example.ordermealservice.entity.vo.ExcelData;
import com.example.ordermealservice.service.OrderClassificationService;

import javax.annotation.Resource;
import java.util.Map;

public class CategoryExcelListener extends AnalysisEventListener<ExcelData> {
    // CategoryExcelListener不能交给spring管理，需要自己new，不能注入其他对象，而new一个对象就需要构造函数
    public OrderClassificationService classificationService;

    public CategoryExcelListener() {
    }

    public CategoryExcelListener(OrderClassificationService classificationService) {
        this.classificationService = classificationService;
    }

    //读取每一行
    @Override
    public void invoke(ExcelData excelData, AnalysisContext analysisContext) {
        if (excelData == null){
            throw new NoqueueException(20001,"导入菜品分类失败");
        }
        OrderClassification orderClassification = new OrderClassification();
        String category = excelData.getCategory();
        String subCategory = excelData.getSubCategory();
        // 一级分类为空，则插入失败
        if (category == null){
            throw new NoqueueException(20001,"导入菜品分类失败");
        }
        // 二级分类为空，则要保证一级分类唯一
        if (subCategory == null){
            LambdaQueryWrapper<OrderClassification> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OrderClassification::getCategory,category);
            int count = classificationService.count(queryWrapper);
            if (count == 0){
                orderClassification.setCategory(category);
                classificationService.save(orderClassification);
                return;
            }else {
                throw new NoqueueException(20001,"导入菜品分类失败");
            }
        }
        // 如果一级分类和二级分类都存在，判断该分类是否存在
        LambdaQueryWrapper<OrderClassification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderClassification::getCategory,category);
        queryWrapper.eq(OrderClassification::getSubCategory,subCategory);
        int count = classificationService.count(queryWrapper);
        if (count == 0){
            LambdaQueryWrapper<OrderClassification> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(OrderClassification::getCategory,category);
            queryWrapper1.last("and sub_category is null");
            classificationService.remove(queryWrapper1);
            orderClassification.setCategory(category);
            orderClassification.setSubCategory(subCategory);
            classificationService.save(orderClassification);
        }else {
            throw new NoqueueException(20001,"导入菜品分类失败");
        }


    }

    //读取表头
    @Override
    public void invokeHeadMap(Map<Integer,String> headMap, AnalysisContext context){
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
